package client

import (
	"github.com/streadway/amqp"
	"fmt"
)

type Client struct {
	routingKeys []string
	queues      []*amqp.Queue
	ch          *amqp.Channel
	notifyCh    chan interface{}
}

func NewClient(routingKeys []string, qs []*amqp.Queue, ch *amqp.Channel) *Client{
	fmt.Println(routingKeys)
	return &Client{
		routingKeys: routingKeys,
		queues:      qs,
		ch:          ch,
		notifyCh:    make(chan interface{}),
	}
}

func(client *Client) PublishMessage() {
	for _,q := range client.queues {
		fmt.Println(q.Name)
		go func(){
			client.handleQueueMsg(q)
		}()
	}
}

func(client *Client) handleQueueMsg(q *amqp.Queue) {
	msgs, err := client.ch.Consume(q.Name, "", true, false, false, false, nil)
	if err != nil {
		panic(err)
	}
	for msg := range msgs {
		fmt.Println("Client consume message")
		client.notifyCh <- msg.Body
	}
}

func(c *Client) GetChannel() chan interface{} {
	return c.notifyCh
}

func(c *Client) GetQueueCount() int{
	return len(c.queues)
}
